import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { TitleModule } from "../../../modules/TitleModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { DataEvent } from "../events/DataEvent";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
import { GameProxy } from "../mvc/GameProxy";
import { GameController } from "../mvc/GameController";
import { LayerManager } from "../manager/LayerManager";
import { ITitleModule } from "../../../modules/interfaces/ITitleModule";
import { STitle } from "../../../Message/Game/STitle";
//class TitleController
    
    export  class TitleController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.TitleEnableTitle, this.onEnableTitle);
            Dispatcher.addEventListener(EventName.TitleHideTitle, this.onHideTitle);
            Dispatcher.addEventListener(EventName.TitleWindowShowOrHide, this.onTitleShowOrHide);
            Dispatcher.addEventListener(EventName.TitleIsShowTitle, this.onTitleIsShowTitle);
            Dispatcher.addEventListener(EventName.TitleIsShowWorldPost, this.onTitleIsShowWorldPost);
            Dispatcher.addEventListener(EventName.GMForbidTalk, this.onGMForbidTalk);
            Dispatcher.addEventListener(EventName.GMUnForbidTalk, this.onGMUnForbidTalk);
            NetDispatcher.addCmdListener(ServerCommand.MyTitlesUpdate, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.TitleGetCommand, this.onTitleGetTips);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._titleModule) 
            {
                this._titleModule = new TitleModule();
                this._titleModule.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._titleModule.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return this._titleModule;
        }

        /* internal  */onViewShow(arg1: WindowEvent): void
        {
            this.update();
            NetDispatcher.addCmdListener(ServerCommand.TitleGetCommand, this.onTilteGet);
            NetDispatcher.addCmdListener(ServerCommand.TitleTimeOut, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.TitleRemove, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.TitleUpdate, this.onTilteUpdate);
            return;
        }

        /* internal  */onViewHide(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TitleGetCommand, this.onTilteGet);
            NetDispatcher.removeCmdListener(ServerCommand.TitleTimeOut, this.onTilteUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.TitleRemove, this.onTilteUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.TitleUpdate, this.onTilteUpdate);
            return;
        }

        /* internal  */onGMForbidTalk(arg1: DataEvent): void
        {
            var loc1=arg1.data as PlayerMiniInfo;
            GameProxy.title.gmForbidTalk(loc1.entityId.id, 1);
            return;
        }

        /* internal  */onGMUnForbidTalk(arg1: DataEvent): void
        {
            var loc1=arg1.data as PlayerMiniInfo;
            GameProxy.title.gmForbidTalk(loc1.entityId.id, 0);
            return;
        }

        /* internal  */onTitleShowOrHide(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                if (GameController.player.isViewShow) 
                {
                    LayerManager.windowLayer.centerWindows(GameController.player.view, view);
                }
                else 
                {
                    view.show();
                }
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onTitleIsShowTitle(arg1: DataEvent): void
        {
            var loc1=Boolean(arg1.data);
            GameProxy.title.notShowTitle(!loc1);
            return;
        }

        /* internal  */onTitleIsShowWorldPost(arg1: DataEvent): void
        {
            var loc1=Boolean(arg1.data);
            GameProxy.title.showCountryTitle(loc1);
            return;
        }

        /* internal  */update(): void
        {
            (view as ITitleModule).updateArray(cache.title.aryTitle);
            return;
        }

        /* internal  */onTilteUpdate(arg1: Object): void
        {
            NetDispatcher.dispatchCmd(ServerCommand.TitleCurrentTitleMaybeChange, null);
            if (this._titleModule) 
            {
                (view as ITitleModule).updateArray(cache.title.aryTitle);
            }
            return;
        }

        /* internal  */onTilteGet(arg1: STitle): void
        {
            NetDispatcher.dispatchCmd(ServerCommand.TitleCurrentTitleMaybeChange, null);
            (view as ITitleModule).updateArray(cache.title.aryTitle);
            return;
        }

        /* internal  */onTitleGetTips(arg1: STitle): void
        {
            (view as ITitleModule).getNewTitle(arg1);
            return;
        }

        /* internal  */onEnableTitle(arg1: DataEvent): void
        {
            GameProxy.title.enableTitle(int(arg1.data));
            return;
        }

        /* internal  */onHideTitle(arg1: DataEvent): void
        {
            GameProxy.title.hideTitle();
            return;
        }

        private /* var */_titleModule: ITitleModule;

        private /* var */_isViewCreate: Boolean=false;
    }
